package com.example.myapplication;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.myapplication.dao.UserDao;
import com.example.myapplication.databinding.ActivityMainBinding;
import com.example.myapplication.db.AppDatabase;
import com.example.myapplication.entity.User;

import java.util.List;

public class MainActivity extends AppCompatActivity {


    private ActivityMainBinding mainBinding;
    private UserDao userDao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userDao = AppDatabase.getInstance(this).getUserDao();
        List<User> userList = DataGenerator.generateUsers();
        userDao.insertUser(userList);


        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mainBinding.btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = mainBinding.etName.getText().toString().trim();
                String pwd = mainBinding.etPassword.getText().toString().trim();
                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
                    Toast.makeText(MainActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                List<User> list = userDao.getUser(name, pwd);
                if (list != null && list.size() > 0) {
                    // 数据库判断
                    Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "登录失败，该用户不存在", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}
